// lazyman

class LazyMan {
    constructor() {
        this.list = []
        setTimeout(async () => {
            for (let i = 0; i < this.list.length; i++) {
                let fn = this.list[i]
                await fn()
            }
        }, 0)
    }

    log = (msg) => {
        this.list.push(() => console.log(msg))
        return this
    }

    sleep = (time) => {
        this.list.push(() => this._delay(time))
        return this
    }

    sleepFirst = (time) => {
        this.list.unshift(() => this._delay(time))
        return this
    }

    _delay = (delay) => {
        return new Promise(reslove => setTimeout(reslove, delay))
    }
}
new LazyMan('jiao').log('18888888').sleep(5000).log('28888888').sleepFirst(3000)

function createFetchWithTimeout(url,params = {}, time){
    return new Promise((reslove, reject) => {
        let controller = new AbortController()

        params.signal = controller.signal 

        let timer = setTimeout(() => {
            controller.abort()
            reject()
        }, time)
        
        fetch(url, params).then(reslove, reject).finally(() => {
            clearTimeout(timer)
        })
    })
}



function task() {
    let list = []
    let timer = null
    let _reject = null
    return {
        run(){
            return new Promise((reslove, reject) => {
                _reject = reject
                timer = setTimeout(reslove, 2000)
            })   
        },
        cancel(){
            clearTimeout(timer)
            _reject()
        }
    }
}